package ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.AnswerCardAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.AnswerCardBean;
import student.andlei.com.student.R;
import utils.GsonUtils;

/**
 * 答题卡
 */
public class AnswerSheetActivity extends BaseNetActivity{
    private TextView tv_title,
                    tv_time,
                    tv_point;
    public AnswerCardBean answerCardBean;
    private Button bt_surecomplete;
    private List<AnswerCardBean.mlist> list = new ArrayList<>();
    private AnswerCardAdapter adapter;
    private LinearLayout layout_weitijiao,layout_pigai;
    private RecyclerView recy_;
    private RelativeLayout relativeLayout;
    private int student_task_id;
    private int answer_id;
    private int status;//批改状态 0未提交 10待批改 100已批改
    private int submit_status;//提交状态 0未提交 10延期提交 100正常提交
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("答题卡");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            if(action.equals("Task/submitAnswer")){
                bt_surecomplete.setVisibility(View.GONE);
                loadData();
            }else {
                answerCardBean = (AnswerCardBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,AnswerCardBean.class);
                tv_title.setText(answerCardBean.paper_name);
                tv_time.setText(answerCardBean.create_time);
                status = answerCardBean.status;
                submit_status = answerCardBean.submit_status;
                //批改状态 0未提交 10待批改 100已批改
                if(status == 0){
                    tv_point.setVisibility(View.GONE);
                }else {
                    tv_point.setText(answerCardBean.score+"分");
                    tv_point.setVisibility(View.VISIBLE);
                }
                //提交状态 0未提交 10延期提交 100正常提交
                if(submit_status == 0){
                    bt_surecomplete.setVisibility(View.VISIBLE);
                    layout_weitijiao.setVisibility(View.VISIBLE);
                    layout_pigai.setVisibility(View.GONE);
                }else {
                    bt_surecomplete.setVisibility(View.GONE);
                    layout_weitijiao.setVisibility(View.GONE);
                    layout_pigai.setVisibility(View.VISIBLE);
                }
                if(answerCardBean.list==null && answerCardBean.list.size()<0){return;}
                LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recy_.setLayoutManager(layoutManager);
                if(submit_status == 0){
                    adapter = new AnswerCardAdapter(AnswerSheetActivity.this,R.layout.item_answersheet, answerCardBean.list,false,student_task_id);
                }else {
                    adapter = new AnswerCardAdapter(AnswerSheetActivity.this,R.layout.item_answersheet, answerCardBean.list,true,student_task_id);
                }
                recy_.setAdapter(adapter);
                relativeLayout.setVisibility(View.VISIBLE);
            }
        }else {
            toast(""+baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_answersheet;
    }

    @Override
    protected void findViews() {
        relativeLayout = getView(R.id.rlayout_);
        tv_title  = getView(R.id.tv_title_plropcre);
        tv_time = getView(R.id.tv_time);
        tv_point = getView(R.id.tv_point);
        recy_ = getView(R.id.rcly_);
        layout_weitijiao = getView(R.id.layout_weitijiao);
        layout_pigai = getView(R.id.layout_pigai);
       // recly_select = getView(R.id.rcly_select);
//                recly_rcly_pack = getView(R.id.rcly_pack);
//                recly_explain = getView(R.id.rcly_explain);
        bt_surecomplete = getView(R.id.bt_surecomplete);
    }

    @Override
    protected void formatViews() {
        bt_surecomplete.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        loadData();
    }
    private void loadData(){
        Map<String,String> map =  new HashMap<>();
        map.put("student_task_id",""+student_task_id);
        map.put("answer_id",""+answer_id);
        post("Task/getAnswerCard",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        student_task_id  = bundle.getInt("student_task_id");
        answer_id = bundle.getInt("answer_id");;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //确认提交
            case R.id.bt_surecomplete:
                Map<String,String> map = new HashMap<>();
                map.put("answer_id",answer_id+"");
                post("Task/submitAnswer",map,BaseBean.class,false);
                break;
        }
    }
}
